/*
 * @Author: Anjg
 * @Date: 2025-07-14 09:23:16
 * @LastEditTime: 2025-07-25 22:00:55
 * @Description:
 */
import Login from '@/api/login';
import { Dialog } from 'vant';
import store from "@/store";
import router from "@/router";
import {request} from "axios";
// 登录
export function login(data) {
    if (process.env.NODE_ENV === 'development') {
        Login.codeLogin(data).then((res) => {
            // 开发环境登录成功时的操作
            if (res.code == 200) {
                store.dispatch('saveUserInfo', res.result);
                localStorage.setItem('userInfo', JSON.stringify(res.result));
                localStorage.setItem('userToken', res.result.token)
            } else {
                Dialog.alert({
                    title: '温馨提示',
                    message: res.Msg || res.errMsg,
                    confirmButtonText: '关闭'
                }).then(() => {
                    logout();
                });
                return Promise.reject(new Error(res.errMsg));
            }
        });
    } else {
        // 生产环境（自动获取ip地址）
        request.defaults.baseUrl = `${取到的ip}/api`
    }
}
// 退出
export async function logout() {
    await store.dispatch('logoutUserInfo')
    localStorage.removeItem('userInfo')
    sessionStorage.clear()
    if (process.env.NODE_ENV === 'development') {
        Login.getLoginOut().then((res) => {
            if (res.code == 200) {
                router.replace({
                    name: 'login'
                });
            } else {
                Dialog.alert({
                    title: '温馨提示',
                    message: res.Msg || res.errMsg,
                    confirmButtonText: '关闭'
                }).then(() => {
                });
                return Promise.reject(new Error(res.errMsg));
            }
        })
    } else {
        // 生产环境
        console.log();
    }
}
